package edu.uta.BillMeApp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;

import edu.BillME.datapars.HttpConnection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/* @auther Siddharth Patel & Susan Foster */
public class Confirmation extends Activity{

	final static String URL="http://omega.uta.edu/~smp3753/email.php"; 
	String getWN, confirm, getFN, amount;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.confirmation);
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        TextView result;
        JSONArray jArray;
  
    	String email1 = null;
        result=(TextView)findViewById(R.id.conftext);
        Button confrm = (Button) findViewById(R.id.conf);
        Button settle = (Button) findViewById(R.id.settle);
        
        Bundle gotBundle=getIntent().getExtras();
        final String friendName = gotBundle.getString("fN");
        getWN=gotBundle.getString("wN");
      
        final String[]split= friendName.split("\\s+");  
		
		String obj = ("Username :"+split[0]+"\nFriends Name :"+split[5]+"\nAmount :"+split[3]+"\n"+split[6]+split[7]+"\n"+split[8]+"\n"+split[9] );
		result.setText(obj);  
		result.setTextColor(Color.WHITE);
		if(split[8].contains("Confirmed")){
			confrm.setVisibility(0);
		}
		else{
			confrm.setEnabled(true);}
		
		 postParameters.add(new BasicNameValuePair("firstname", split[0]));
        //paring data
        try{
        	 jArray = new JSONArray(HttpConnection.executeHttpPost(URL,postParameters));
        	 
             for(Integer i=0; i< jArray.length(); i++){
                 try{
                     //Get My JSONObject and grab the String Value that I want.
                     email1 = jArray.getJSONObject(i).getString("emailId");
    
                 }catch(JSONException e){
                  Toast.makeText(getBaseContext(), "email address not recovered" ,Toast.LENGTH_LONG).show();
                 }
                                  
       
             }

         }catch (Exception e1) {
          Log.e("log_tag", "Error final1212 "+e1.toString());
          }

		confrm.setOnClickListener(new View.OnClickListener(){

			//@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
				postParameters.add(new BasicNameValuePair("billid", split[7]));

				String response = null;
				
				if(split[5].equals(getWN)){
				
					try {
						response = HttpConnection.executeHttpPost("http://omega.uta.edu/~smp3753/confirmbill.php", postParameters);
				
						String res=response.toString();
						res= res.replace("\n","");
						if(res.equals("1")){
							Toast.makeText(getApplicationContext(), "Bill confirmed" ,Toast.LENGTH_LONG).show();
						}
						else{
							Toast.makeText(getApplicationContext(), "Error in connection" ,Toast.LENGTH_LONG).show();
						}

					}catch (Exception e) {
					
					}
					}else{
						Toast.makeText(getApplicationContext(), "You cannot confirm this." ,Toast.LENGTH_LONG).show();
					}
			}
		});
		
		
		final String emailid= email1;
		final String msg= obj;
		
		settle.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				
			Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
			String[] recipients = new String[]{emailid, "",};
			emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Billme Reminder");
			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,msg );
			emailIntent.setType("text/plain");
			startActivity(Intent.createChooser(emailIntent, "Send mail..."));
			finish();
			}

		});	
	}
}